/**
 *
 */

package com.moogene.saad.sns.ui;

import com.moogene.android.R;
import com.moogene.saad.sns.Friend;
import com.moogene.saad.sns.Session;
import com.moogene.saad.sns.provider.SocialGraph;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 28850924
 *
 */
public class FriendsChooserActivity extends Activity {

    List<Friend> mFriends;

    class FriendItemsAdapter extends BaseAdapter {

        /*
         * (non-Javadoc)
         *
         * @see android.widget.Adapter#getCount()
         */
        @Override
        public int getCount() {
            return mFriends.size();
        }

        /*
         * (non-Javadoc)
         *
         * @see android.widget.Adapter#getItem(int)
         */
        @Override
        public Object getItem(int position) {
            return mFriends.get(position);
        }

        /*
         * (non-Javadoc)
         *
         * @see android.widget.Adapter#getItemId(int)
         */
        @Override
        public long getItemId(int position) {
            return position;
        }

        /*
         * (non-Javadoc)
         *
         * @see android.widget.Adapter#getView(int, android.view.View,
         * android.view.ViewGroup)
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Friend friend = (Friend) getItem(position);
            View rowView = null;
            if (convertView == null) {

                LayoutInflater inflater = FriendsChooserActivity.this.getLayoutInflater();
                rowView = inflater.inflate(R.layout.social_friends_chooser_list_item, null,
                        true);
                ImageView gravator = (ImageView)rowView.findViewById(R.id.ItemImage);
                TextView title = (TextView)rowView.findViewById(R.id.ItemTitle);
                TextView text = (TextView)rowView.findViewById(R.id.ItemText);

                title.setText(friend.getDisplayName());
                text.setText(friend.getMobile());
                title.setTextColor(Color.BLACK);

                //gravator.setImageBitmap(friend.getGravator());

                gravator.setImageResource(R.drawable.android);

                rowView.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT,150));
                gravator.layout(0, 0, 50, 50);
                rowView.setBackgroundColor(Color.WHITE);

            } else {
                rowView = convertView;
                ImageView gravator = (ImageView)rowView.findViewById(R.id.ItemImage);
                TextView title = (TextView)rowView.findViewById(R.id.ItemTitle);
                TextView text = (TextView)rowView.findViewById(R.id.ItemText);

                title.setText(friend.getDisplayName());
                text.setText(friend.getMobile());

                gravator.setImageBitmap(friend.getGravator());
                gravator.setImageResource(R.drawable.android);

//                rowView.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT,150));
//                rowView.setBackgroundColor(Color.WHITE);
            }
            return rowView;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.social_friends_chooser_listview);
        loadContactFriends();
        ListView list = (ListView)findViewById(R.id.friends_list_view);
        list.setAdapter(new FriendItemsAdapter());
    }

    private void loadContactFriends() {
        mFriends = SocialGraph.getContactFriends(this);
    }
}
